﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Swan.WakeUpLibrary;

namespace Swan.SwanWakeUp.Controls
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class Settings : UserControl
    {
        public Settings()
        {
            InitializeComponent();
            this.DataContext = Properties.Settings.Default;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".mp3";
            dlg.Filter = "Text documents (.mp3)|*.mp3";

            // Display OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                Properties.Settings.Default.CustomSongPath = filename;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            // Test run
            var alarm = new Alarm("\\" + Properties.Settings.Default.AlarmName);
            if (alarm.IsActive)
                alarm.Kill();
            else if (!alarm.Run())
                MessageBox.Show("No alarm is defined, please create an alarm first");
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var thisProc = System.Diagnostics.Process.GetCurrentProcess();
            var proc = System.Diagnostics.Process.GetProcesses().Where(p => p.ProcessName.ToLower().Equals("swanwakeup") && p.Id != thisProc.Id).FirstOrDefault();
            proc.Kill();
        }
    }
}
